package mil.af.amc.adpetracker.web.actions.struts1.MaintenanceEntry;

import mil.af.amc.adpetracker.model.MaintenanceEntry;
import mil.af.amc.adpetracker.service.MaintenanceService;
import mil.af.amc.adpetracker.web.actions.struts1.AbstractDispatchAction;
import org.apache.struts.action.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Please fill in this javadoc
 *
 * @author TSgt Jason Ferguson
 */
public class MaintenanceEntryDispatchAction extends AbstractDispatchAction {

    private MaintenanceService maintenanceService;

    public void setMaintenanceService(MaintenanceService maintenanceService) {
        this.maintenanceService = maintenanceService;
    }


    public ActionForward viewCreateMaintenanceEntry(ActionMapping actionMapping,
                                                    ActionForm actionForm,
                                                    HttpServletRequest httpServletRequest,
                                                    HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewCreateMaintenanceEntry");
    }

    public ActionForward createMaintenanceEntry(ActionMapping actionMapping,
                                ActionForm actionForm,
                                HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;
        ActionMessages errors = form.validate(actionMapping, httpServletRequest);
        if (!errors.isEmpty()) {
            saveErrors(httpServletRequest, errors);
            actionMapping.findForward("viewCreateMaintenanceEntry");
        }

        int id = Integer.parseInt(httpServletRequest.getParameter("itemId"));
        form.set("itemId", id);
        MaintenanceEntry maint = maintenanceService.create(form.getMap());

        return actionMapping.findForward("successCreateMaintenanceEntry");

    }

}
